/*
 * Animation.cpp
 *
 *  Created on: 09-06-2013
 *      Author: Jarek
 */

#include "EAnimation.h"

Animation::Animation() :
	prevState		(BEHAVIOUR_MAX),
	postState		(BEHAVIOUR_MAX),
	startTime		(0.0f),
	endTime			(0.0f)
{

}

Animation::~Animation() {

}

Animation::BEHAVIOUR Animation::mapAnimationState(aiAnimBehaviour behaviour)
{
	switch(behaviour)
	{
		case aiAnimBehaviour_CONSTANT:
			return BEHAVIOUR_DEFAULT;
		case aiAnimBehaviour_DEFAULT:
			return BEHAVIOUR_STATIC;
		case aiAnimBehaviour_REPEAT:
			return BEHAVIOUR_REPEAT;
		default:
			assert(false);
	}
}

Vector3<float> Animation::getTranslationData(float time)
{
	if(time <= startTime)
		return keys.translationKeys[0].key;
	if(time >= endTime)
		return keys.translationKeys[keys.translationKeysCount-1].key;

	for (uint i = 0; i < keys.translationKeysCount - 1; i++)
	{
		if(time >= keys.translationKeys[i].time && time <= keys.translationKeys[i+1].time)
		{
			const Key<Vector3<float> > &k1 = keys.translationKeys[i];
			const Key<Vector3<float> > &k2 = keys.translationKeys[i+1];

			float diff		= k2.time;
			float offset	= time - k1.time;
			float coeff		= offset / diff;	//range <0, 1>
			return Vector3<float>(LERP(coeff, k1.key.x, k2.key.x), LERP(coeff, k1.key.y, k2.key.y), LERP(coeff, k1.key.z, k2.key.z));
		}
	}
	return keys.translationKeys[0].key;
}

Quaternion<float> Animation::getRotationData(float time)
{
	if (time <= startTime)
		return keys.rotationKeys[0].key;
	if (time >= endTime)
		return keys.rotationKeys[keys.rotationKeysCount-1].key;

	for (uint i = 0; i < keys.rotationKeysCount - 1; ++i)
	{
		if (time >= keys.rotationKeys[i].time && time <= keys.rotationKeys[i+1].time)
		{
			const Key< Quaternion<float> > &k1 = keys.rotationKeys[i];
			const Key< Quaternion<float> > &k2 = keys.rotationKeys[i+1];
			float dif 		= k2.time - k1.time;
			float offset	= time - k1.time;
			float coeff		= offset / dif; 	// range <0, 1>
			return slerp(coeff, k1.key, k2.key);
		}
	}

	return keys.rotationKeys[0].key;
}

Vector3<float> Animation::getScalingData(float time)
{
	if (time <= startTime)
		return keys.scalingKeys[0].key;
	if (time >= endTime)
		return keys.scalingKeys[keys.scalingKeysCount-1].key;

	for (uint i = 0; i < keys.scalingKeysCount - 1; ++i)
	{
		if (time >= keys.scalingKeys[i].time && time <= keys.scalingKeys[i+1].time)
		{
			const Key< Vector3<float> > &k1 = keys.scalingKeys[i];
			const Key< Vector3<float> > &k2 = keys.scalingKeys[i+1];
			float dif 		= k2.time - k1.time;
			float offset	= time - k1.time;
			float coeff		= offset / dif; 	// range <0, 1>
			return Vector3<float>(LERP(coeff, k1.key.x, k2.key.x), LERP(coeff, k1.key.y, k2.key.y), LERP(coeff, k1.key.z, k2.key.z));
		}
	}

	return keys.scalingKeys[0].key;
}

